package de.dopichaj.labrador.util.html;

/**
 * Helper methods for the html-display of results
 * @author Benedikt
 */
public class HTMLDisplay {
    
    /**
     * Replaces all XML-Tags with their entities and surrounds them by a span
     * of class "xml", so the style can be adjusted in the css.
     */
    private static String highlightXML(String xmlFragment) {
        String retVal = null;
        retVal = xmlFragment.replaceAll("<(.*?)>","<span class=\"xml\">&lt;$1&gt;</span>");
        return retVal;
    }
    
    /**
     * Replaces all XML-Tags with a whitespace
     */
    private static String discardXML(String xmlFragment) {
        String retVal = null;
        retVal = xmlFragment.replaceAll("<(.*?)>"," ");
        return retVal;
    }
    
    /**
     * Surrounds the given term inside the fragment with a span of class
     * "term", so the style can be adjusted in the css. However, terms may
     * be highlighted that are partial words, as in "filepath", when searching
     * has been done for "file". This may not be what the searcher has been 
     * looking for, although this sometimes is an effect of stemming. 
     * May currently also highlight xml, which may be taken care of
     * by the stylesheet (.xml .term ...)
     */
    private static String highlightSearchTerm(String xmlFragment, String term) {
        String retVal = null;
        String regEx = "(?i)(.*?)(" + term + ")(.*?)";
        retVal = xmlFragment.replaceAll(regEx,"$1<span class=\"term\">$2</span>$3");
        return retVal;        
    }
    
    
    /**
     * Highlights search terms and xml. Note that in case of trying to do both,
     * XML-highlighting must always be applied first!!!
     */
    public static String applyHighlighting(String xmlFragment, String[] terms) {
        return applyHighlighting(xmlFragment, terms, false);
    }
    
    public static String applyHighlighting(String xmlFragment, String[] terms, boolean displayXML) {
        String retVal = xmlFragment;
        if(displayXML) {
            retVal = highlightXML(retVal);
        } else {
            retVal = discardXML(retVal);
        }
        for(int i=0; i < terms.length; i++) {
            retVal = highlightSearchTerm(retVal, terms[i]);
        }
        return retVal;        
    }
}
/*
Copyright (c) 2005 Benedikt Eger
Copyright (c) 2005-2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/